﻿using BXH.Movie.Bll;
using BXH.Movie.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BXH.Movie.Model;
using BXH.Movie.FrmUsers;
using BXH.Movie.FrmAdmin;

namespace BXH.Movie
{
    public partial class FrmLogin : CCWin.CCSkinMain
    {
        private readonly UserInfoService userInfoService = new UserInfoService();
        public event EventHandler LoginSuccess;
        public Form MainForm { get; private set; }//这个是属性
        public FrmLogin()
        {
            //小驼峰 
            //大驼峰
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            FrmRegisterAndAddAdmin frmRegister = new FrmRegisterAndAddAdmin(RoleType.User);
            frmRegister.ShowDialog();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            var userCode = txtUserCode.Text.Trim();
            var password = Encrypt.MD5Encrypt16(txtPassword.Text);//加密
            var userInfo = userInfoService.Login(userCode, password);//登录 如果数据库可以查到对象那么登录成功
            if (userInfo != null)//判断返回对象是否为空
            {
                Program.LoginUser = userInfo;
                this.LoginSuccess?.Invoke(this,new EventArgs());
                this.DialogResult = DialogResult.OK;
             
            }
            else
                MessageBox.Show("账号或者密码错误");
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            this.txtPassword.Text = "";
        }
    }
}
